"""
Test Configuration
==================

Pytest configuration and fixtures for testing.
"""

import pytest
import os
import sys
from datetime import datetime, timedelta

# Add project root to path
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

from app import create_app, db
from app.models.user import User
from app.utils.database import DatabaseManager

@pytest.fixture
def app():
    """Create application for testing."""
    app = create_app('testing')
    
    with app.app_context():
        db.create_all()
        yield app
        db.session.remove()
        db.drop_all()

@pytest.fixture
def client(app):
    """Create test client."""
    return app.test_client()

@pytest.fixture
def database_manager(app):
    """Create database manager for testing."""
    return DatabaseManager(db)

@pytest.fixture
def sample_user(app, database_manager):
    """Create a sample user for testing."""
    user = User(
        username='testuser',
        email='test@example.com',
        first_name='Test',
        last_name='User'
    )
    user.set_password('TestPass123!')
    
    database_manager.create(user)
    return user

@pytest.fixture
def auth_headers(app, sample_user):
    """Get authentication headers for testing."""
    from app.utils.jwt_handler import jwt_handler
    
    tokens = jwt_handler.generate_tokens(
        user_id=sample_user.id,
        username=sample_user.username,
        email=sample_user.email
    )
    
    return {
        'Authorization': f'Bearer {tokens["access_token"]}'
    }

@pytest.fixture
def admin_user(app, database_manager):
    """Create an admin user for testing."""
    user = User(
        username='admin',
        email='admin@example.com',
        first_name='Admin',
        last_name='User',
        is_active=True,
        is_verified=True
    )
    user.set_password('AdminPass123!')
    
    database_manager.create(user)
    return user